  <template>
    <div class="boxCard">
      <el-card class="box-card">
        <div class="title">
          <span />
          <p>查询条件</p>
        </div>
        <el-form label-width="120px">
          <el-row :gutter="12">
            <el-col :span="6">
              <el-form-item label="用户名: ">
                <el-input v-model="seachObj.userName" placeholder="请输入用户名" />
              </el-form-item>
            </el-col>

            <el-col :span="18" style="text-align: right">
              <el-button type="primary" @click="getInit(1)">查询</el-button>
              <el-button @click="rest">重置</el-button>
            </el-col>
          </el-row>
        </el-form>
      </el-card>

      <el-card class="box-card" style="width: 100%; height: 80%">
        <div class="header">
          <div class="title">
            <span />
            <p>用户列表</p>
          </div>

          <div>
            <el-button type="primary" size="small" @click="showAddForm">新增</el-button>
          </div>
        </div>

        <el-table v-loading="loading" :data="userList" height="85%">
          <el-table-column type="selection" width="55" align="center" fixed="left" />
          <el-table-column type="index" width="60" align="center" label="序号" fixed="left" />"
          <el-table-column label="用户名称" align="center" prop="userName" />
          <el-table-column label="用户类型" align="center" prop="roleName" />
          <el-table-column label="账号" align="center" prop="accountNumber" />
          <el-table-column label="密码" align="center" prop="password" />
          <el-table-column label="地址" align="center" prop="address" width="300" />
          <el-table-column label="注册时间" align="center" prop="createTime" width="200" />
          <el-table-column label="最近一次登录时间" align="center" width="200">
            <template slot-scope="scope">
              <span>{{ scope.row.lastLoginTime || "---" }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="120" fixed="right">
            <template slot-scope="scope">
              <el-button type="text" @click="updateData(scope.row)">修改</el-button>
              <el-button type="text" @click="del(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <div style="text-align: right; margin-top: 10px">
          <el-pagination :current-page="seachObj.page" :page-sizes="[10, 50, 100, 200]" :page-size="seachObj.size"
            layout="total, sizes, prev, pager, next, jumper" :total="seachObj.total" @size-change="handleSizeChange"
            @current-change="handleCurrentChange" />
        </div>
      </el-card>

      <!-- 新增弹出框 -->
      <el-dialog title="新增用户" :visible.sync="dialogTableVisible">
        <el-form ref="addFrom" label-width="120px" :model="addObj" :rules="rules">
          <el-row :gutter="12">
            <el-col :span="24">
              <el-form-item label="用户名称: " prop="userName">
                <el-input v-model="addObj.userName" placeholder="请输入用户名称" />
              </el-form-item>
            </el-col>

            <el-col :span="24">
              <el-form-item label="用户类型: " prop="roleCode">
                <el-select v-model="addObj.roleCode" placeholder="请选择用户类型" style="width: 100%">
                  <el-option value="1" label="超级管理员" />
                  <el-option value="2" label="管理员" />
                  <el-option value="3" label="普通用户" />
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="24">
              <el-form-item label="账号: " prop="accountNumber">
                <el-input v-model="addObj.accountNumber" placeholder="请输入用户名称" />
              </el-form-item>
            </el-col>

            <el-col :span="24">
              <el-form-item label="密码: " prop="password">
                <el-input v-model="addObj.password" placeholder="请输入用户名称" />
              </el-form-item>
            </el-col>

            <el-col :span="24">
              <el-form-item label="地址: " prop="address">
                <el-input v-model="addObj.address" placeholder="请输入用户名称" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>

        <div style="margin-top: 10px; text-align: center">
          <el-button type="primary" @click="confirm">确定</el-button>
          <el-button @click="dialogTableVisible = false">取消</el-button>
        </div>
      </el-dialog>
    </div>
  </template>

<script>
import { addUserInfo, deleteUser, getUserList } from "@/api/user";

export default {
  data() {
    return {
      seachObj: {
        page: 1,
        size: 10,
        total: 0,
      },
      userList: [],
      loading: false,
      dialogTableVisible: false,

      addObj: {},

      rules: {
        userName: [
          { required: true, message: "请输入用户名称", trigger: "blur" },
        ],
        accountNumber: [
          { required: true, message: "请输入账号", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        address: [
          { required: true, message: "请输入用户地址", trigger: "blur" },
        ],
        roleCode: [
          { required: true, message: "请选择用户类型", trigger: "change" },
        ],
      },
    };
  },

  mounted() {
    this.getInit();
  },

  methods: {
    async getInit(page) {
      this.seachObj.page = page || this.seachObj.page;
      this.loading = true;
      const res = await getUserList(this.seachObj);
      this.loading = false;
      console.log(res);
      if (res.code == 20000) {
        this.userList = res.data || [];
        this.seachObj.total = res.total;
      } else {
        this.$message.error(res.msg);
      }
    },

    // 重置按钮
    rest() {
      this.seachObj = {};
      this.getInit();
    },

    // 点击新增按钮
    showAddForm() {
      this.dialogTableVisible = true;
      this.addObj = {};
    },

    // 点击修改按钮
    updateData(row) {
      this.dialogTableVisible = true;
      this.addObj = JSON.parse(JSON.stringify(row));
    },

    // 点击确定按钮
    confirm() {
      // 表单校验
      this.$refs.addFrom.validate(async (valid) => {
        if (valid) {
          this.$confirm("确定提交吗？", "提示", {
            cancelButtonText: "取消",
            confirmButtonText: "确定",
            type: "warning",
          }).then(async () => {
            const result = await addUserInfo(this.addObj);
            if (result.code == 20000) {
              this.$message.success("提交成功");
              this.getInit();
            } else {
              this.$message.error(result.msg);
            }
            this.dialogTableVisible = false;
          });
        }
      });
    },

    // 删除
    del(row) {
      this.$confirm("确定删除吗？", "提示", {
        cancelButtonText: "取消",
        confirmButtonText: "确定",
        type: "warning",
      }).then(async () => {
        console.log(row.id);
        const result = await deleteUser({ id: row.id });
        if (result.code == 20000) {
          this.$message.success("删除成功");
          this.getInit();
        } else {
          this.$message.error(result.msg);
        }
      });
    },
    // 分页相关
    handleSizeChange(e) {
      this.seachObj.size = e;
      this.getInit();
    },
    handleCurrentChange() {
      this.seachObj.page = e;
      this.getInit();
    },
  },
};
</script>

<style scoped>
>>>.el-card__body {
  padding: 20px;
  width: 100%;
  height: 100%;
}
</style>
